package com.starlinkdt.common.utils.oaiauth;

import com.starlinkdt.common.constant.HttpStatus;
import com.starlinkdt.common.domain.system.entity.OaiAcc;
import com.starlinkdt.common.domain.system.entity.OaiPerm;
import com.starlinkdt.common.domain.system.entity.OaiServ;
import com.starlinkdt.common.enums.DisableStatus;
import com.starlinkdt.common.exception.ServiceException;
import com.starlinkdt.common.mapper.system.OaiAccMapper;
import com.starlinkdt.common.mapper.system.OaiPermMapper;
import com.starlinkdt.common.mapper.system.OaiServMapper;
import com.starlinkdt.common.utils.JSONUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * OAI账户权限校验
 *
 * @author asset
 */
@Component
public class OaiServAuthPermUtils {
    @Autowired
    private OaiAccMapper oaiAccMapper;

    @Autowired
    private OaiServMapper oaiServMapper;

    @Autowired
    private OaiPermMapper oaiPermMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    private static final String REDIS_KEY_PRE = "openapi:auth:perm:";

    public OaiAcc getOaiAcc(String accAppId) {
        OaiAcc oaiAcc = new OaiAcc();

        // 获取缓存
        String vAccRedisKey = REDIS_KEY_PRE + "acc:" + accAppId;
        String vAcc = redisTemplate.opsForValue().get(vAccRedisKey);
        if (vAcc == null || vAcc.isEmpty()) {
            oaiAcc = oaiAccMapper.selectOaiAccByAccAppId(accAppId);
            oaiAcc = oaiAcc == null ? new OaiAcc() : oaiAcc;
            redisTemplate.opsForValue().set(vAccRedisKey, JSONUtils.toJSONString(oaiAcc), 600, TimeUnit.SECONDS);
        } else {
            oaiAcc = JSONUtils.parseObject(vAcc, OaiAcc.class);
        }

        // 有效性验证
        if (oaiAcc.getAccBizNo() == null || oaiAcc.getAccBizNo().isEmpty()) {
            throw new ServiceException("OAI账户不存在", HttpStatus.NOT_FOUND);
        }
        if (oaiAcc.getStatus().equals(DisableStatus.DISABLE.getType())) {
            throw new ServiceException("OAI账户已禁用", HttpStatus.FORBIDDEN);
        }

        return oaiAcc;
    }

    public OaiServ getOaiServ(String requestUri) {
        OaiServ oaiServ = new OaiServ();

        // 获取缓存
        String vServRedisKey = REDIS_KEY_PRE + "serv:" + requestUri;
        String vServ = redisTemplate.opsForValue().get(vServRedisKey);
        if (vServ == null || vServ.isEmpty()) {
            oaiServ = oaiServMapper.selectOaiServByServApi(requestUri);
            oaiServ = oaiServ == null ? new OaiServ() : oaiServ;
            redisTemplate.opsForValue().set(vServRedisKey, JSONUtils.toJSONString(oaiServ), 600, TimeUnit.SECONDS);
        } else {
            oaiServ = JSONUtils.parseObject(vServ, OaiServ.class);
        }

        // 有效性验证
        if (oaiServ.getServBizNo() == null || oaiServ.getServBizNo().isEmpty()) {
            throw new ServiceException("OAI服务不存在", HttpStatus.NOT_FOUND);
        }
        if (oaiServ.getStatus().equals(DisableStatus.DISABLE.getType())) {
            throw new ServiceException("OAI服务已禁用", HttpStatus.FORBIDDEN);
        }

        return oaiServ;
    }

    public OaiPerm getOaiPerm(String accBizNo, String servBizNo) {
        OaiPerm oaiPerm = new OaiPerm();

        // 获取缓存
        String vPermRedisKey = REDIS_KEY_PRE + "perm:" + accBizNo + "-" + servBizNo;
        String vPerm = redisTemplate.opsForValue().get(vPermRedisKey);
        if (vPerm == null || vPerm.isEmpty()) {
            oaiPerm = oaiPermMapper.selectOaiPermByAccServ(accBizNo, servBizNo);
            oaiPerm = oaiPerm == null ? new OaiPerm() : oaiPerm;
            redisTemplate.opsForValue().set(vPermRedisKey, JSONUtils.toJSONString(oaiPerm), 600, TimeUnit.SECONDS);
        } else {
            oaiPerm = JSONUtils.parseObject(vPerm, OaiPerm.class);
        }

        // 有效性验证
        if (oaiPerm.getPermBizNo() == null || oaiPerm.getPermBizNo().isEmpty()) {
            throw new ServiceException("OAI权限不存在", HttpStatus.NOT_FOUND);
        }
        if (oaiPerm.getStatus().equals(DisableStatus.DISABLE.getType())) {
            throw new ServiceException("OAI权限已禁用", HttpStatus.FORBIDDEN);
        }

        return oaiPerm;
    }

}
